package com.example.mystoryapp2.ui.story.create

import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.example.mystoryapp2.R
import com.example.mystoryapp2.contexts.StoryContext
import com.example.mystoryapp2.data.network.ServiceCreator
import com.example.mystoryapp2.databinding.ActivityCreateStoryBinding
import com.example.mystoryapp2.enums.EnumEditStoryMode
import com.example.mystoryapp2.ui.base.BaseActivity
import com.example.mystoryapp2.ui.story.page.PageActivity
import com.example.mystoryapp2.ui.utils.DialogUtils

class CreateStoryActivity : BaseActivity<ActivityCreateStoryBinding, CreateStoryViewModel>() {

    companion object {
        fun startEditInstance(ctx: Context, id: Int) {
            StoryContext.createActWorkMode = EnumEditStoryMode.EDIT
            StoryContext.storyId = id
            startIntent(ctx)
        }

        fun startCreateInstance(ctx: Context) {
            StoryContext.createActWorkMode = EnumEditStoryMode.CREATE
            StoryContext.storyId = -1
            startIntent(ctx)
        }



        private fun startIntent(ctx: Context) {
            val intent = Intent(ctx, CreateStoryActivity::class.java)
            ctx.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initData()
        showBackTitle()
    }

    private fun initData() {
        db.viewModel?.initWorkMode { it, b ->
            when (it) {
                EnumEditStoryMode.EDIT -> {
                    initSp()
                    db.btnSave.visibility = View.VISIBLE
                    db.btnCreateStory.visibility = View.GONE
                    db.btnAddPage.visibility = View.VISIBLE
                    db.spCategory.isEnabled = false
                }

                EnumEditStoryMode.CREATE -> {
                    db.btnSave.visibility = View.GONE
                    db.btnCreateStory.visibility = View.VISIBLE
                    db.btnAddPage.visibility = View.GONE
                    db.spCategory.isEnabled = true
                }
            }
        }
    }

    private fun initSp() {
        db.viewModel?.initStory {
            db.spCategory.setSelection(it)
        }
    }

    override fun initView() {
        db.btnCreateStory.setOnClickListener {
            db.viewModel?.create(object : ServiceCreator.ICallBack<Boolean> {
                override fun onSuccess(res: Boolean) {
                    DialogUtils.showLongMessage(this@CreateStoryActivity, R.string.tip_success)
                    this@CreateStoryActivity.finish()
                }
            })
        }
        db.btnCancel.setOnClickListener { cancel() }
        db.btnSave.setOnClickListener {
            db.viewModel?.save(object : ServiceCreator.ICallBack<Boolean> {
                override fun onSuccess(res: Boolean) {
                    DialogUtils.showLongMessage(this@CreateStoryActivity, R.string.tip_success)
                    this@CreateStoryActivity.finish()
                }
            })
        }
        db.btnAddPage.setOnClickListener {
            newPage()
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_create_story
    }

    override fun getViewModel(): CreateStoryViewModel {
        return ViewModelProvider(
            this, ViewModelProvider
                .AndroidViewModelFactory(application)
        )[CreateStoryViewModel::class.java]
    }

    override fun setViewModel() {
        db.viewModel = getViewModel()
    }


    private fun cancel() {
        DialogUtils.showTwoBtn(this@CreateStoryActivity,
            getString(R.string.title_confirm_cancel_story_creation),
            getString(R.string.title_tip),
            object : DialogUtils.ICallBack {
                override fun onPositive(dialog: DialogInterface) {
                    dialog.dismiss()
                    finish()
                }
            })
    }

    private fun newPage() {
        if (db.viewModel?.hasPage==true) {
            db.viewModel?.getFirstPage {
                PageActivity.startEditInstance(this@CreateStoryActivity, it)
            }
        } else {
            PageActivity.startCreateInstance(this@CreateStoryActivity)
        }
    }


}
